package com.xiaomo.gitee.datamanager.controller.user;

import com.xiaomo.gitee.datamanager.common.result.Code;
import com.xiaomo.gitee.datamanager.common.result.Result;
import com.xiaomo.gitee.datamanager.dto.statistics.StatisticsDataDTO;
import com.xiaomo.gitee.datamanager.service.statistics.StatisticsService;
import com.xiaomo.gitee.datamanager.service.statistics.impl.StatisticsServiceImpl;
import com.xiaomo.gitee.datamanager.util.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
@Tag(name = "获取统计数据信息",description = "获取大类别或数据集图片数量信息")
@RequiredArgsConstructor
public class UserStatisticsController {

    private final StatisticsService statisticsService;

    @GetMapping("/statistics/parent-category")
    @PreAuthorize("hasAuthority('statistics_info')")
    @Operation(
            summary = "获取大类别统计数据",
            description = "获取统计数据传输模型的列表,包含每个大类别的图片数量等信息",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "获取成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    array = @ArraySchema(
                                            schema = @Schema(
                                                    implementation = StatisticsDataDTO.class
                                            )
                                    )
                            )
                    )
            }
    )
    public Result<List<StatisticsDataDTO>> getParentClassStatistics() {
        return Result.ok(statisticsService.getParentClassStatistics());
    }

    @GetMapping("/statistics/child-category/{id}")
    @PreAuthorize("hasAuthority('statistics_info')")
    @Operation(
            summary = "获取数据集统计数据",
            description = "获取统计数据传输模型的列表,包含每个数据集的图片数量等信息",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "id",
                            description = "大类别ID",
                            required = true,
                            in = ParameterIn.PATH
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "获取成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    array = @ArraySchema(
                                            schema = @Schema(
                                                    implementation = StatisticsDataDTO.class
                                            )
                                    )
                            )
                    )
            }
    )
    public Result<List<StatisticsDataDTO>> getChildClassStatistics(@PathVariable Long id) {
        return Result.ok(statisticsService.getChildClassStatistics(id));
    }

    @GetMapping("/statistics/image")
    @PreAuthorize("hasAuthority('statistics_info')")
    @Operation(
            summary = "获取图片总数量",
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    array = @ArraySchema(
                                            schema = @Schema(
                                                    implementation = StatisticsDataDTO.class
                                            )
                                    )
                            )
                    )
            }
    )
    public Result<Long> getImageAmount() {
        return  Result.ok(statisticsService.getImagesAmount());
    }


}
